package com.example.its2021606134chh.BusDiyModule;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.its2021606134chh.BusModule.Bus;
import com.example.its2021606134chh.BusModule.BusLists;
import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.MainActivity;
import com.example.its2021606134chh.R;
import com.google.gson.Gson;

import java.util.Collections;
import java.util.Comparator;

public class BusConfirmOrderActivity extends AppCompatActivity {

    private ImageView imgBusConfirmOrderBack;
    private TextView tvBusConfirmOrderStartEnd;
    private TextView tvBusConfirmOrderUser;
    private TextView tvBusConfirmOrderPhone;
    private TextView tvBusConfirmOrderStartPlace;
    private TextView tvBusConfirmOrderEndPlace;
    private TextView tvBusConfirmOrderDate;
    private Button btnBusConfirmOrderSubmit;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus_confirm_order);
        initView();
        Intent intent1 = getIntent();
        String id = intent1.getStringExtra("id");
        String start_place = intent1.getStringExtra("start_place");
        String end_place = intent1.getStringExtra("end_place");
        String real_start_place = intent1.getStringExtra("real_start_place");
        String real_end_place = intent1.getStringExtra("real_end_place");
        String price = intent1.getStringExtra("price");
        String user = intent1.getStringExtra("user");
        String phone_number = intent1.getStringExtra("phone_number");
        String busstop_date = intent1.getStringExtra("busstop_date");
        tvBusConfirmOrderStartEnd.setText("乘车路线：" + start_place + "—" + end_place);
        tvBusConfirmOrderUser.setText(user);
        tvBusConfirmOrderPhone.setText(phone_number);
        tvBusConfirmOrderStartPlace.setText(real_start_place);
        tvBusConfirmOrderStartPlace.setText(real_end_place);
        tvBusConfirmOrderDate.setText(busstop_date);
        btnBusConfirmOrderSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                            @Override
                            public void onFailure(Exception e) {

                            }

                            @Override
                            public void onResponse(String json) {
                                Log.i("TAG", json);
                                Gson gson = new Gson();
                                ConFirmOrder order = gson.fromJson(json, ConFirmOrder.class);
                                String RESULT = order.getRESULT();
                                if (RESULT.equals("S")) {
                                    Toast.makeText(BusConfirmOrderActivity.this, "订单创建成功", Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(BusConfirmOrderActivity.this, MainActivity.class));
                                    finish();
                                } else {
                                    Toast.makeText(BusConfirmOrderActivity.this, "订单创建失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, "set_busline_order", new String[]{"UserName", "bus_id", "user", "phone_number", "busstop_start", "busstop_end", "busstop_date"}, "user1", id, user, phone_number, real_start_place, real_end_place, busstop_date);
                    }
                }).start();
            }
        });
    }

    private void initView() {
        imgBusConfirmOrderBack = (ImageView) findViewById(R.id.imgBusConfirmOrderBack);
        tvBusConfirmOrderStartEnd = (TextView) findViewById(R.id.tvBusConfirmOrderStartEnd);
        tvBusConfirmOrderUser = (TextView) findViewById(R.id.tvBusConfirmOrderUser);
        tvBusConfirmOrderPhone = (TextView) findViewById(R.id.tvBusConfirmOrderPhone);
        tvBusConfirmOrderStartPlace = (TextView) findViewById(R.id.tvBusConfirmOrderStartPlace);
        tvBusConfirmOrderEndPlace = (TextView) findViewById(R.id.tvBusConfirmOrderEndPlace);
        tvBusConfirmOrderDate = (TextView) findViewById(R.id.tvBusConfirmOrderDate);
        btnBusConfirmOrderSubmit = (Button) findViewById(R.id.btnBusConfirmOrderSubmit);
    }
}